package com.atguigui.leetcode1;

/**
 * 2490. 回环句
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/6/30 10:06
 */
public class P2490CircularSentence {
    public static void main(String[] args) {

    }

    class Solution {
        public boolean isCircularSentence(String sentence) {


            String[] str = sentence.split(" ");
            int n = str.length;
            char first = str[0].charAt(0);
            char last = ' ';

            if (str.length <= 1) {
                int l = str[0].length();
                return first == str[0].charAt(l - 1);
            }

            for (int i = 0; i < n; i++) {
                int l = str[i].length();
                if (i == 0) {
                    last = str[i].charAt(l - 1);
                    continue;
                }

                if (i == n - 1) {
                    if (str[i].charAt(0) != last) {
                        return false;
                    } else {
                        if (first != str[i].charAt(l - 1)) {
                            return false;
                        }
                    }
                }

                if (str[i].charAt(0) != last) {
                    return false;
                } else {
                    last = str[i].charAt(l - 1);
                }

            }

            return true;

        }
    }
}
